<?php

namespace validate;

class Login extends \TC\IO\Input
{
    //验证规则
    protected $rule = [
        'mobile' => 'require|mobile',
        'code' => 'require|length:6',
        'account' => 'require',
        'password' => 'require|max:32|min:6',
        'repassword' => 'require|confirm:password',
    ];
    //验证消息
    protected $message = [
        'mobile.require' => '手机号必须填写',
        'account.require' => '用户名/手机号必须填写',
        'account.length' => '用户名为2到10位',
        'mobile.mobile' => '请检查手机号格式是否正确',
        'code.require' => '手机验证码不能为空',
        'code.length' => '手机验证码长度必须是6位',
        'password.require' => '密码必须填写',
        'password.min' => '密码必须在6-32位之间',
        'password.max' => '密码必须在6-32位之间',
        'repassword.require' => '确认密码不能为空',
        'repassword.confirm' => '两次密码输入不一致',
    ];
    //验证场景
    protected $scene = [
        'login_ap' => ['account', 'password'],
        'login_mp' => ['mobile', 'password'],
        'register' => ['mobile', 'code', 'password', 'repassword'],
        'login_code' => ['mobile', 'code'],
        'mobile' => ['mobile']
    ];

    /**
     * @desc 自定义验证用户名/手机号
     * @return mixed
     * @author zhaozhiwei
     * @date 2021-02-04 13:40
     */
    public static function checkAccount()
    {

        $params = \TC\Di\Container::get('Yaf_Com_Dispatcher')->getRequest()->getPost();
        if (empty($params)) {
            \TC\IO\Output::OverError('用户名/手机号必须填写');
        }
        $account = $params['account'];
        $mobile = '/^1[3-9][0-9]\d{8}$/';
        preg_match_all($mobile, $account, $match);
        if (empty($match[0]) && !(strlen($account) > 2 && strlen($account) < 12)) {
            \TC\IO\Output::OverError('手机号/用户名格式不正确');
        }
        return $params;
    }

}